import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_risk_object_by_id_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_security_identification_page/risk_security_identification_controller.dart';
import 'package:get/get.dart';

///风险管控-》安全风险辨识-》点击+新增界面 对象新增
class RiskOnClickAddPageController extends GetxController {
  ///责任部门
  String departmentName = '';

  ///责任部门ID
  String departmentId = '';

  ///责任人
  String username = '';

  ///责任人ID
  String id = '';

  ///辨识部门
  String bDepartmentName = '';

  ///辨识部门ID
  String bDepartmentId = '';

  ///辨识人
  String bDusername = '';

  ///辨识人ID
  String bDid = '';

  ///风险分析对象名称
  String riskObjectName = '';

  ///默认新增
  bool addAndUpdate = true;

  @override
  void onInit() {
    super.onInit();
    if (Get.arguments != null) {
      addAndUpdate = false;
      getRiskObjectById();
    }
  }

  ///新增或更新风险分析对象
  void addRiskObject() async {
    if (riskObjectName.isEmpty) {
      AppToast.showWarning('请输入风险分析对象名称');
      return;
    }
    if (departmentName.isEmpty) {
      AppToast.showWarning('请选择责任部门');
      return;
    }
    if (username.isEmpty) {
      AppToast.showWarning('请选择责任人');
      return;
    }
    if (bDepartmentName.isEmpty) {
      AppToast.showWarning('请选择辨识部门');
      return;
    }
    if (bDusername.isEmpty) {
      AppToast.showWarning('请选择辨识人');
      return;
    }

    // 新增或更新风险分析对象
    final res = await HttpUtil().generatePostRequest(Interface.addRiskObject, data: {
      'hazardDepId': departmentId,
      'hazardDistinguishDepId': bDepartmentId,
      'hazardDistinguishPersonId': bDid,
      'hazardLiablePersonId': id,
      'id': addAndUpdate ? null : Get.arguments,
      'riskObjectName': riskObjectName
    }).execute(isShowLoading: true);
    res.onSuccess((result) {
      Get.back();
      // 刷新列表
      final RiskSecurityIdentificationController riskController = Get.find();
      riskController.globalKey.currentState?.onRefresh();
    });
    res.onError((result) {
      AppToast.showError('新增失败~');
    });
  }

  /// 请求参数
  Map<String, dynamic> queryParameters = {};

  ///获取相应数据 对象
  GetRiskObjectByIdEntity? data;

  ///获取指定的风险对象 详情--赋值
 void getRiskObjectById() async {
    queryParameters['id'] = Get.arguments;
    final res = await HttpUtil()
        .generateGetRequest<GetRiskObjectByIdEntity>(
          Interface.getRiskObjectById,
          queryParameters: queryParameters,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });

    departmentName = data?.hazardDep ?? '';
    departmentId = data?.hazardDepId ?? '';
    username = data?.hazardLiablePerson ?? '';
    id = data?.hazardLiablePersonId ?? '';
    bDid = data?.hazardDistinguishPersonId ?? '';
    riskObjectName = data?.riskObjectName ?? '';
    bDepartmentName = data?.hazardDistinguishDep ?? '';
    bDepartmentId = data?.hazardDistinguishDepId ?? '';
    bDusername = data?.hazardDistinguishPerson ?? '';
    update();
  }

  void isUpdate() {
    update();
  }
}
